import { getShopSupply, getAllBaseInfo } from "../../../Api/productionmanage/index"
import { getSupplierInfo } from '../../../Api/productionmanage/supplier_info'
// 获取商家投入品信息 接口

const productionInfo = {
    namespaced: true,
    state: () => ({
        baseSupplyInfo: [],    //基地投入品库存信息
        ShopSupplyInfo: [],      //商家投入品信息
        baseInfoList: [],        //企业基地
        suppliersList: [],       //供应商数组
        specificationList: [
            {
                id: 1,
                name: '50kg/包',
            },
            {
                id: 2,
                name: '50g/包'
            },
            {
                id: 3,
                name: '1升/支'
            }
        ],
        typeList: [
            {
                id: 1,
                name: "入库申请",
            },
            {
                id: 2,
                name: "出库申请",
            },
        ],
        gradeList: [         //等级规格
            {
                id: 1,
                value: "一等品",
            },
            {
                id: 2,
                value: "二等品",
            },
            {
                id: 3,
                value: "三等品",
            },
        ],
        columns: [
            { name: "投入品", show: false, prop: 'name' },
            { name: "投入品供应商", show: false, prop: 'supplierName' },
            { name: "投入品品种", show: true, prop: 'species' },
            { name: "投入品类型", show: true, prop: 'type' },
            { name: "SKU编号", show: true, prop: 'skuNumber' },
            { name: "剩余质量", show: true, prop: 'surplusWeight' },
            { name: "剩余数量", show: true, prop: 'surplusAmount' },
            { name: "单位", show: false, prop: 'unit' },
            { name: "单价(RMB)", show: false, prop: 'unitPrice' },
            { name: "包装类型", show: false, prop: 'packageType' },
            { name: "规格", show: false, prop: 'specification' },
            { name: "投入品二维码", show: false, prop: 'qrCode' },
            { name: "是否上架", show: false, prop: 'onShelf' },
        ],
        // 公共校验规则
        rules: {
            amount: [
                {
                    required: true,
                    message: `请输入数量`,
                    trigger: "blur"
                }
            ],
            weight: [
                {
                    required: true,
                    message: `请输入质量`,
                    trigger: "blur"
                }
            ],
            supplyName: [
                {
                    required: true,
                    message: `请输入投入品`,
                    trigger: "blur"
                }
            ],
            supplierName: [
                {
                    required: true,
                    message: `请填写供应商`,
                    trigger: "blur"
                }
            ],
            type: [
                {
                    required: true,
                    message: `投入品类型`,
                    trigger: "blur"
                }
            ],
            skuNumber: [
                {
                    required: true,
                    message: `请填写SKU编号`,
                    trigger: "blur"
                }
            ],
            supplierShortName: [
                {
                    required: true,
                    message: `请填写供应商简称`,
                    trigger: "blur"
                }
            ],
            legalPerson: [
                {
                    required: true,
                    message: `请填写法人代表`,
                    trigger: "blur"
                }
            ],
            contactMethod: [
                {
                    required: true,
                    message: `请填写联系方式`,
                    trigger: "blur"
                }
            ],
            contactPerson: [
                {
                    required: true,
                    message: `请填写联系人`,
                    trigger: "blur"
                }
            ],
            businessScope: [
                {
                    required: true,
                    message: `请填写经营范围`,
                    trigger: "blur"
                }
            ],
            type: [
                {
                    required: true,
                    message: `请填写企业类型`,
                    trigger: "blur"
                }
            ],
            type1: [
                {
                    required: true,
                    message: `请填写投入品类型`,
                    trigger: "blur"
                }
            ],
            businessBackupsCode: [
                {
                    required: true,
                    message: `请填写商务部备号`,
                    trigger: "blur"
                }
            ],
            introduction: [
                {
                    required: true,
                    message: `请填写简介`,
                    trigger: "blur"
                }
            ],
            houseNumber: [
                {
                    required: true,
                    message: `请填写详细地址`,
                    trigger: "blur"
                }
            ],
            businessRegistrationNumber: [
                {
                    required: true,
                    message: `请填写工商注册号`,
                    trigger: "blur"
                }
            ],
            packageType: [
                {
                    required: true,
                    message: `请填写包裹类型`,
                    trigger: "blur"
                }
            ],
            unit: [
                {
                    required: true,
                    message: `请填写单位`,
                    trigger: "blur"
                }
            ],
            unitPrice: [
                {
                    required: true,
                    message: `请填写单价`,
                    trigger: "blur"
                }
            ],
            grade: [
                {
                    required: true,
                    message: `请选择投入品等级`,
                    trigger: "blur"
                }
            ],
            specification: [
                {
                    required: true,
                    message: `请填写投入品规格`,
                    trigger: "blur"
                }
            ],
            bestTasteDay: [
                {
                    required: true,
                    message: `请填写最佳口感天数`,
                    trigger: "blur"
                }
            ],
            expirationDate: [
                {
                    required: true,
                    message: `请填写保质期`,
                    trigger: "blur"
                }
            ],
            baseId: [
                {
                    required: true,
                    message: `请选择基地`,
                    trigger: "blur"
                }
            ]
        },
        layout: 'search, refresh, column', // 表头右侧显示功能
        // 离开页面是data的数据被复原 重新进入是直接赋值个页面中data的page
        page: {
            current: 1,
            pageSize: 10,
            total: 0
        },
    }),
    getters: {
        // 获取库存信息
        info: (state) => state.ShopSupplyInfo,
        // 获取校验规则 
        rules: (state) => state.rules
    },
    mutations: {
        // 保存商家投入品数据
        setshopsupplyinfo(state, e) {
            state.ShopSupplyInfo = e
        },
        // 保存基地投入品信息
        setBaseSupplyInfo(state, e) {
            state.baseSupplyInfo = e
        },
        //  保存基地信息
        setBaseInfo(state, e) {
            state.baseInfoList = e
        },
        // 保存请求中的total
        settotal(state, e) {
            state.page.total = e
        },
        // 保存分页的数据
        setsupplyinfopage(state, e) {
            state.page = e
        },
        setSupplierInfo(state, e) {
            state.suppliersList = e
        }
    },
    actions: {
        //在APP.vue首次调用获取 
        // context 包含store的state和mutations的方法
        //获取商家的投入品信息
        getSupplyInfo(context) {
            //该action 请求最后会返回 res 的结果或err
            return new Promise((resolve, reject) => {
                getShopSupply().then(res => {
                    context.commit('setshopsupplyinfo', res.data.data)
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        //获取基地已有投入品信息
        // getBaseSupplyInfo(context){
        //     return new Promise((resolve, reject)=>{
        //         getBaseSupply().then(res =>{
        //             context.commit('setBaseSupplyInfo',res.data.data)
        //             resolve(res)
        //         }).catch(err=>{
        //             reject(err)
        //         })
        //     })
        // }
        //获取所有基地信息
        getBaseInfo(context) {
            return new Promise((resolve, reject) => {
                getAllBaseInfo().then(res => {
                    context.commit('setBaseInfo', res.data.data)
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },

        getSuppliersList(context) {
            return new Promise((resolve, reject) => {
                getSupplierInfo().then(res => {
                    context.commit('setSupplierInfo', res.data.data)
                    resolve(res)
                }).catch(err => {
                    reject(err);
                })
            })
        }

        // 保存分页
        // supplyinfopage(context, page) {
        //     context.commit('setsupplyinfopage', page)
        // }
        // 获取商家
    }
}

export default productionInfo